<?php
/**
 * 执行类
 * Created by PhpStorm.
 * User: xiao
 * Date: 2018/3/19
 * Time: 14:01
 */

namespace Org\Baidu;


class Exec
{

    public static function doIt()
    {
        $loginService = new LoginService(StatisticsConfig::LOGIN_URL(), StatisticsConfig::UUID());

        // 预先登录
        if (!$loginService->preLogin(StatisticsConfig::USERNAME(), StatisticsConfig::TOKEN())) die();

        // doLogin
        $ret = $loginService->doLogin(StatisticsConfig::USERNAME(), StatisticsConfig::PASSWORD(), StatisticsConfig::TOKEN());

        if (!$ret) die();

        $ucid = $ret['ucid'];
        $st = $ret['st'];

        $reportService = new ReportService(StatisticsConfig::API_URL(), StatisticsConfig::USERNAME(),
            StatisticsConfig::TOKEN(), $ucid, $st);

        // get site list
        $ret = $reportService->getSiteList();
        dump($ret['raw'] . PHP_EOL);
        $siteList = $ret['body']['data'][0]['list'];

        if (count($siteList) > 0) {
            $siteId = $siteList[0]['site_id'];

            $sendData = array(
                'site_id' => $siteId,                       // 站点ID
//                'method' => 'overview/getCommonTrackRpt', //趋势分析报告
                'method' => 'visit/toppage/a', //趋势分析报告
                'start_date' => '20180320',             //所查询数据的起始日期
                'end_date' => '20180320',               //所查询数据的结束日期
                'metrics' => 'pv_count,visitor_count',  //所查询指标为PV和UV
                'max_results' => 0,                     //返回所有条数
            );

            $ret = $reportService->getData($sendData);

            dump($ret['raw'] . PHP_EOL);
        }

        // doLogout
        $loginService->doLogout(StatisticsConfig::USERNAME(), StatisticsConfig::TOKEN(), $ucid, $st);

    }


}